package com.woniu.his.mms.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.his.mms.infra.model.DrugModel;
import com.woniu.his.mms.infra.mapper.DrugModelMapper;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.his.mms.infra.model.HerbalModelItem;
import com.woniu.his.mms.infra.model.MedicineModelItem;
import com.woniu.his.mms.service.DrugModelService;
import com.woniu.his.mms.service.HerbalModelItemService;
import com.woniu.his.mms.service.MedicineModelItemService;
import com.woniu.his.mms.service.dto.DmsHerbalModelItemDto;
import com.woniu.his.mms.service.dto.DmsMedicineModelItemDto;
import com.woniu.his.mms.service.dto.DrugModelDto;
import com.woniu.his.mms.service.dto.DrugModelPageDto;
import com.woniu.his.mms.service.param.CreateDrugModelServiceParam;
import com.woniu.his.mms.service.param.DmsHerbalModelItemServiceParam;
import com.woniu.his.mms.service.param.DmsMedicineModelItemServcieParam;
import com.woniu.his.mms.service.param.PageDrugModelServiceParam;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 药品-模板 服务实现类
 * </p>
 *
 * @author ZJH
 * @since 2022-11-28
 */
@Service
public class DrugModelServiceImpl extends ServiceImpl<DrugModelMapper, DrugModel> implements DrugModelService {


    @Resource
    private HerbalModelItemService herModelItemService;

    @Resource
    private MedicineModelItemService medicineModelItemService;

    /**
     * 创建模板
     *
     * @param param
     * @return
     */
    @Override
    public boolean createDrugModel(CreateDrugModelServiceParam param) {
        DrugModel drugModel = BeanUtil.copyProperties(param, DrugModel.class);
        drugModel.setCreateTime(new Date());
        drugModel.setStatusDrugModel("ON");
        save(drugModel);
        if (param.getType().equals("HELBAL")) {
            //这是草药
            List<DmsHerbalModelItemServiceParam> dmsHerbalModelItemList = param.getDmsHerbalModelItemList();
            for (DmsHerbalModelItemServiceParam dmsHerbalModelItemServiceParam : dmsHerbalModelItemList) {
                HerbalModelItem herbalModelItem = BeanUtil.copyProperties(dmsHerbalModelItemList, HerbalModelItem.class);
                herbalModelItem.setStatusHerbal("ON");
                herbalModelItem.setModelId(drugModel.getId());
                herModelItemService.save(herbalModelItem);
            }
        } else if (param.getType().equals("MEDICINE")) {
            //这是成药
            List<DmsMedicineModelItemServcieParam> dmsMedicineModelItemList = param.getDmsMedicineModelItemList();
            for (DmsMedicineModelItemServcieParam dmsMedicineModelItemServcieParam : dmsMedicineModelItemList) {
                MedicineModelItem medicineModelItem = BeanUtil.copyProperties(dmsMedicineModelItemServcieParam, MedicineModelItem.class);
                medicineModelItem.setModelId(drugModel.getId());
                medicineModelItem.setStatusMedicine("ON");
                medicineModelItemService.save(medicineModelItem);
            }
        }
        return true;
    }

    /**
     * 删除目录
     * @param ids
     * @return
     */
    @Override
    public boolean deleteDrugModel(List<Long> ids) {
        List<DrugModel> drugModels = listByIds(ids);
        if(drugModels.size()<=0) return false;
        for (DrugModel drugModel : drugModels) {
            drugModel.setStatusDrugModel("OFF");
        }
        boolean b = updateBatchById(drugModels);
        List<HerbalModelItem> model_id = herModelItemService.list(new QueryWrapper<HerbalModelItem>().in("model_id", ids));
        for (HerbalModelItem herbalModelItem : model_id) {
            herbalModelItem.setStatusHerbal("OFF");
        }
        boolean b2 = herModelItemService.updateBatchById(model_id);
        List<MedicineModelItem> model_id1 = medicineModelItemService.list(new QueryWrapper<MedicineModelItem>().in("model_id", ids));
        for (MedicineModelItem medicineModelItem : model_id1) {
            medicineModelItem.setStatusMedicine("OFF");
        }
        boolean b1 = medicineModelItemService.updateBatchById(model_id1);
        return b&&b1&&b2;
    }

    @Override
    public boolean updateDrugModel(CreateDrugModelServiceParam param) {
        DrugModel drugModel = BeanUtil.copyProperties(param, DrugModel.class);
        if(drugModel.getId()==null) return false;
        boolean b = updateById(drugModel);

        List<DmsHerbalModelItemServiceParam> dmsHerbalModelItemList = param.getDmsHerbalModelItemList();
        for (DmsHerbalModelItemServiceParam dmsHerbalModelItemServiceParam : dmsHerbalModelItemList) {
            HerbalModelItem herbalModelItem = BeanUtil.copyProperties(dmsHerbalModelItemServiceParam, HerbalModelItem.class);
            herModelItemService.updateById(herbalModelItem);
        }
        List<DmsMedicineModelItemServcieParam> listq = param.getDmsMedicineModelItemList();
        for (DmsMedicineModelItemServcieParam dmsMedicineModelItemServcieParam : listq) {
            MedicineModelItem medicineModelItem = BeanUtil.copyProperties(dmsMedicineModelItemServcieParam, MedicineModelItem.class);
            medicineModelItemService.updateById(medicineModelItem);
        }

        return b;
    }

    @Override
    public DrugModelPageDto listPageDrugModel(PageDrugModelServiceParam param) {
        QueryWrapper<DrugModel> wrapper = new QueryWrapper<>();
        if(param.getScope()!=null)wrapper.eq("scope",param.getScope());
        wrapper.eq("status_drug_model","OFF");
        if(param.getType()!=null) wrapper.eq("type",param.getType());
        if(param.getCodeDrugModel()!=null) wrapper.eq("code_drug_model",param.getCodeDrugModel());
        if(param.getNameDrugModel()!=null) wrapper.eq("name_drug_model",param.getNameDrugModel());
        if(param.getAim()!=null) wrapper.eq("aim",param.getAim());
        Page<DrugModel> page = page(new Page<DrugModel>(param.getIndexPage(), param.getPageSize()), wrapper);
        DrugModelPageDto dto = new DrugModelPageDto();
        dto.setIndexPage(page.getCurrent());
        dto.setTotal(page.getTotal());
        dto.setModelList(BeanUtil.copyToList(page.getRecords(), DrugModelDto.class));


        List<DrugModelDto> modelList = dto.getModelList();
        for (DrugModelDto drugModelDto : modelList) {
            if(drugModelDto.getType().equals("HELBAL")){
                //草药模板
                List<HerbalModelItem> model_id = herModelItemService.list(new QueryWrapper<HerbalModelItem>().eq("model_id", drugModelDto.getId()));
                drugModelDto.setDmsHerbalModelItemList(BeanUtil.copyToList(model_id, DmsHerbalModelItemDto.class));
            }else {
                //成药模板
                List<MedicineModelItem> model_id = medicineModelItemService.list(new QueryWrapper<MedicineModelItem>().eq("model_id", drugModelDto.getId()));
                drugModelDto.setDmsMedicineModelItemList(BeanUtil.copyToList(model_id, DmsMedicineModelItemDto.class));
            }
        }
        return dto;
    }
}
